<template>
  <div>
    <el-card shadow="hover" style="margin: 20px 0; width:100%">
      <el-form class="demo-form-inline">
        <el-form-item label="SPU名称" label-width="80px">：{{ spu.spuName }}</el-form-item>
        <el-form-item label="SKU名称" label-width="80px">
          <el-input placeholder="请输入SKU名称" style="width:150px" v-model="skuInfo.skuName"></el-input>
        </el-form-item>
        <el-form-item label="价格(元)" label-width="80px">
          <el-input placeholder="请输入SKU价格" style="width:150px" v-model="skuInfo.price"></el-input>
        </el-form-item>
        <el-form-item label="重量(千克)" label-width="80px">
          <el-input placeholder="请输入SKU重量" style="width:150px" v-model="skuInfo.weight"></el-input>
        </el-form-item>
        <el-form-item label="规格描述" label-width="80px">
          <el-input style="width:100%" type="textarea" :rows="10" placeholder="SKU规格描述" v-model="skuInfo.skuDesc">
          </el-input>
        </el-form-item>
        <el-form-item label="平台属性" label-width="80px">
          <el-form :inline="true" label-width="80px">
            <el-form-item :label="attr.attrName" v-for="attr in attrInfoList" :key="attr.id">
              <el-select placeholder="请选择" v-model="attr.AttridAndValueid">
                <el-option :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`"
                  v-for="attrValue in attr.attrValueList" :key="attrValue.id"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-form-item>
        <el-form-item label="销售属性" label-width="80px">
          <el-form :inline="true" label-width="80px">
            <el-form-item :label="spuSaleAttr.saleAttrName" v-for="spuSaleAttr in spuSaleAttrList"
              :key="spuSaleAttr.id">
              <el-select placeholder="请选择" v-model="spuSaleAttr.SpusaleidandValueid">
                <el-option :label="spuSaleAttrValue.saleAttrValueName"
                  :value="`${spuSaleAttr.id}:${spuSaleAttrValue.id}`"
                  v-for="spuSaleAttrValue in spuSaleAttr.spuSaleAttrValueList" :key="spuSaleAttrValue.id"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-form-item>
        <el-form-item label="图片列表" style="margin-top:50px">
          <el-table stripe style="width: 100%;margin-left: 80px;" border :data="spuImageList"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" prop="prop" width="80" align="center">
            </el-table-column>
            <el-table-column label="图片" width="180" align="center">
              <template slot-scope="{ row }">
                <img :src="row.imgUrl" alt="" style="width:100px;height:100px">
              </template>
            </el-table-column>
            <el-table-column prop="imgName" label="名称" width="580" align="center">
            </el-table-column>
            <el-table-column prop="address" label="操作" align="center">
              <template slot-scope="{ row }">
                <el-button type="primary" icon="el-icon-delete" size="mini" @click="getisDefault(row, spuImageList)"
                  v-if="row.isDefault == '0'">设为默认</el-button>
                <el-button type="success" icon="el-icon-lock" size="mini" v-else>默认图片</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-button type="success" style="margin-left:80px;margin-top: 20px;" icon="el-icon-folder-add" @click="save">保存
        </el-button>
        <el-button type="info" style="margin-top: 20px;margin-left:30px;" icon="el-icon-folder-delete" @click="SkuInfo">
          取消
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'SkuForm',
  data() {
    return {
      //存储图片的信息
      spuImageList: [],
      //存储销售是属性
      spuSaleAttrList: [],
      //存储平台属性的数据
      attrInfoList: [],
      //收集sku数据的字段
      skuInfo: {
        //第一类收集的数据：父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类：需要通过数据双向绑定v-model收集
        skuName: "",
        price: 0,
        weight: "",
        skuDesc: "",
        //第三类：需要自己书写代码
        //默认图片
        skuDefaultImg: "",
        //收集图片的字段
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   skuId: 0,
          //   spuImgId: 0,
          // },
        ],
        //平台属性
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0,
          // },
        ],
        //销售属性
        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: "string",
          //   saleAttrValueId: 0,
          //   saleAttrValueName: "string",
          //   skuId: 0,
          //   spuId: 0,
          // },
        ],


      },
      spu: {},
      //收集图片的数据字段:但是需要注意，收集的数据目前缺少isDefault字段，将来提交给服务器数据的时候，需要整理参数
      imageList: []
    };
  },
  methods: {
    SkuInfo() {
      this.$message.info('取消成功')
      Object.assign(this._data, this.$options.data());

      this.$emit('changerSKu', 0)
    },
    async getSkuInfo(category1Id, category2Id, spu) {
      this.spu = spu
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      let SpuImageresult = await this.$API.reqSpuImageLIst(spu.id)
      if (SpuImageresult.code == 200) {
        let list = SpuImageresult.data
        list.forEach(item => {
          item.isDefault = '0'
        })
        this.spuImageList = list
      }
      let SpuSaleresult = await this.$API.reqSpuSaleAttrList(spu.id)
      if (SpuSaleresult.code == 200) {
        this.spuSaleAttrList = SpuSaleresult.data
      }
      let AttrInforesult = await this.$API.reqAttrInfoList(category1Id, category2Id, spu.id)
      if (AttrInforesult.code == 200) {
        this.attrInfoList = AttrInforesult.data
      }
    },
    handleSelectionChange(params) {
      this.imageList = params
    },
    getisDefault(row, spuImageList) {
      spuImageList.forEach(item => item.isDefault = '0')
      row.isDefault = '1'
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    async save() {
      const { attrInfoList, skuInfo } = this
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.AttridAndValueid) {
          const [attrId, valueId] = item.AttridAndValueid.split(':')
          prev.push({ attrId, valueId })
        }
        return prev
      }, [])

      skuInfo.skuSaleAttrValueList = this.spuSaleAttrList.reduce((prev, item) => {
        if (item.SpusaleidandValueid) {
          const [saleAttrId, saleAttrValueId] = item.SpusaleidandValueid.split(':')
          prev.push({ saleAttrId, saleAttrValueId })
        }
        return prev
      }, [])
      skuInfo.skuImageList = this.imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        }
      })
      let result = await this.$API.reqAddSku(skuInfo)
      console.log(result);
      if (result.code == 200) {
        this.$message.success('添加sku成功')
        Object.assign(this._data, this.$options.data());
        this.$emit('changerSKu', 0)

      }
    }

  }
}
</script>

<style>
</style>